package com.spring.boot.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.spring.boot.utils.DataSourceUtils;
import com.spring.boot.utils.MultipleDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: springboot_vue
 * @description: druid连接池配置
 * @author: ry
 * @create: 2018-08-15 15:53
 **/
@Configuration
public class DuridConfig {

    @Value("${spring.security.user.name}")
    private String username;

    @Value("${spring.security.user.password}")
    private String password;

    @Value("${spring.datasource.logSlowSql}")
    private String logSlowSql;

    @Value("${ry.datasource.name}")
    private String [] dataPeoperties;

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", username);
        reg.addInitParameter("loginPassword", password);
        reg.addInitParameter("logSlowSql", logSlowSql);
        return reg;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        return filterRegistrationBean;
    }

    /**
     * 配置多数据源
     * @return
     * @throws IOException
     * @throws SQLException
     */
    public MultipleDataSource setMultipleDataSource() throws IOException, SQLException {
        MultipleDataSource multipleDataSource=new MultipleDataSource();
        DataSource qzdata= DataSourceUtils.setQuartzDatesource("quartz.properties");
        //设置quartz数据源为默认数据源
        multipleDataSource.setDefaultTargetDataSource(qzdata);
        Map<Object,Object> map=new HashMap<>();
        map.put("quartz", qzdata);
        for (String pro : dataPeoperties) {
            map.put(pro, DataSourceUtils.setDruidDatesource(pro+".properties"));
        }
        //日志数据源
        //map.put("logs", DataSourceUtils.setDruidDatesource("logs.properties"));
        //配置为多数据源
        multipleDataSource.setTargetDataSources(map);
        return multipleDataSource;
    }



    @Bean
    public DataSource druidDataSource() {
        try {
            return setMultipleDataSource();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
